package com.google.sky.ui;

import com.google.sky.Board;
import com.google.sky.MoveResolver;
import com.google.sky.Position;
import com.google.sky.ai.simple.Heuristic;
import com.google.sky.ai.simple.MoveResolverImpl;

/**
 * Console UI for testing AI.
 *
 * @author sslautin
 * @version 1.0 22.11.2014
 */
public class ConsoleApp {

    public static void main(final String[] args) {

        final Board board = new ArrayBoard(15, 15);
        final MoveResolver resolver = new MoveResolverImpl();
        System.out.println("Lets play!");
        System.out.println();
        printBoard((ArrayBoard) board, 0, 0);
        while (!Heuristic.isTerminalNode(((ArrayBoard) board).array)) {
            final Position position = resolver.nextStep(board);
            if (((ArrayBoard) board).currentBlack) {
                ((ArrayBoard) board).array[position.getX()][position.getY()] = 1;
            } else {
                ((ArrayBoard) board).array[position.getX()][position.getY()] = 2;
            }
            ((ArrayBoard) board).switchPlayer();
            printBoard((ArrayBoard) board, position.getX(), position.getY());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Game over!");
    }

    private static void printBoard(final ArrayBoard arrayBoard, final int lastX, final int lastY) {

        final int[][] array = arrayBoard.array;
        for (int x = 0; x < arrayBoard.width; x++) {
            for (int y = 0; y < arrayBoard.height; y++) {
                final int p = array[x][y];
                if (p == 1) {
                    if (x == lastX && y == lastY) {
                        System.out.print("_\u2744_");
                    } else {
                        System.out.print(" \u2744 ");
                    }
                } else if (p == 2) {
                    if (x == lastX && y == lastY) {
                        System.out.print("_O_");
                    } else {
                        System.out.print(" O ");
                    }
                } else {
                    System.out.print(" . ");
                }
            }
            System.out.println();
            System.out.println();
        }
        System.out.println();
    }

    private static class ArrayBoard implements Board {

        private final int[][] array;
        private final int width;
        private final int height;

        private boolean currentBlack = true;

        private ArrayBoard(final int width, final int height) {

            array = new int[width][height];
            this.width = width;
            this.height = height;
        }

        @Override
        public Position getPositionAt(final int x, final int y) {

            if (x < 0 || y < 0 || x >= width || y >= height) {
                return null;
            } else {
                final int p = array[x][y];
                return new Position() {

                    @Override
                    public int getX() {
                        return x;
                    }

                    @Override
                    public int getY() {
                        return y;
                    }

                    @Override
                    public boolean isMine() {

                        if (currentBlack) {
                            return p == 1;
                        } else {
                            return p == 2;
                        }
                    }

                    @Override
                    public boolean isEmpty() {
                        return p == 0;
                    }
                };
            }
        }

        @Override
        public int getWidth() {
            return width;
        }

        @Override
        public int getHeight() {
            return height;
        }

        public void switchPlayer() {
            currentBlack = !currentBlack;
        }
    }
}
